<?php
/**********************数据库操作功能性工具******************************/
/**
 * @Desc     SQL 字符串格式化，带SQL注入问题
 * @Author   pekey
 * @DateTime 2020-05-12T19:27:56+0800
 * @return   [type]
 * ex:
 *     $v = sql_format("aaa{0}...{1}",$a,$b);
 */
function sql_format()
{
    $CI   = &get_instance();
    $args = func_get_args();
    if (count($args) == 0) {return;}
    if (count($args) == 1) {return $args[0];}
    $str = array_shift($args);
    $match=function ($match) use ($args){
        return isset($args[$match[1]]) ? $args[$match[1]] : $match[0];
    };
    $str = preg_replace_callback('/\\{(0|[1-9]\\d*)\\}/',$match, $str);
    return $str;
}

/**
 * @Desc     like类型SQL 字符串格式化，带SQL注入问题
 * @Author   pekey
 * @DateTime 2020-05-12T19:28:39+0800
 * @return   [type]
 * ex:
 *     $v = sql_like_format("aaa{0}...{1}",$a,$b);
 */
function sql_like_format()
{
    $CI   = &get_instance();
    $args = func_get_args();
    if (count($args) == 0) {return;}
    if (count($args) == 1) {return $args[0];}
    $str = array_shift($args);
    $match=function ($match) use ($args){
        return isset($args[$match[1]]) ? $args[$match[1]] : $match[0];
    };
    $str = $str = preg_replace_callback('/\\{(0|[1-9]\\d*)\\}/', $match,$str);
    return $str;
}

/**
 * [array_sql_in 对数组进行in拼接]
 * @Description 对数组进行in拼接
 * @Author      FengPQ
 * @DateTime    2020-05-14T09:05:34+0800
 * @param       [type]                   $v [数组对象]
 * @return      [type]                      [带，的拼接结果]
 */
function array_sql_in($v)
{
    if (is_array($v) && count($v) != 0) {
        return "'" . str_replace(",", "','", join(",", $v)) . "'";
    } else if (is_array($v)) {
        return $v[0];
    }
    return $v;

}

